<?php

namespace App\Services\Huanxin;

class UserServices extends BaseService
{
    /**
     * 授权注册单个用户
     * $username 用户环信ID
     * $password 用户密码
     * return false 或者true
     */
    public function createUser($username, $password = '123456')
    {
        if (empty($username) || empty($password)) {
            return false;
        }
        $url = $this->url . 'users';
        $options = array(
            "username" => $username,
            "password" => $password
        );
        $body = json_encode($options);
        $header = array($this->getToken());
        $data = postCurl($url, $body, $header);
        return resultHuanxinInfo($data);
    }

    /**
     * 获取单个用户详细信息
     *$username 用户环信ID
     * return false 或者true
     */
    public function getUser($username)
    {
        if (empty($username)) {
            return false;
        }
        $url = $this->url . 'users/' . $username;
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, "GET");
        return resultHuanxinInfo($data);
    }

    /**
     * 删除单个用户
     *$username 用户环信ID
     * return false 或者true
     */
    public function deleteUser($username)
    {
        if (empty($username)) {
            return false;
        }
        $url = $this->url . 'users/' . $username;
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'DELETE');
        return resultHuanxinInfo($data);
    }

    /**
     * 修改用户密码
     *$username 用户环信ID
     * $newpassword 用户新密码
     * return false 或者true
     */
    public function resetPassword($username, $newpassword)
    {
        if (empty($username) || empty($newpassword)) {
            return false;
        }
        $url = $this->url . 'users/' . $username . '/password';
        $options = array(
            "newpassword" => $newpassword
        );
        $body = json_encode($options);
        $header = array($this->getToken());
        $data = postCurl($url, $body, $header, "PUT");
        return resultHuanxinInfo($data);
    }

    /**
     * 修改用户昵称
     * $username 用户环信ID
     * $nickname 用户昵称
     * return false 或者true
     */
    public function editNickname($username, $nickname)
    {
        if (empty($username) || empty($nickname)) {
            return false;
        }
        $url = $this->url . 'users/' . $username;
        $options = array(
            "nickname" => $nickname
        );
        $body = json_encode($options);
        $header = array($this->getToken());
        $data = postCurl($url, $body, $header, 'PUT');
        return resultHuanxinInfo($data);
    }

    /**
     * 设置推送消息展示方式
     * $username 用户环信ID
     * $notification_display_style  “0”仅通知，“1“通知以及消息详情
     * return false 或者true
     *
     */
    public function editPushType($username, $notification_display_style)
    {
        if (empty($username) || empty($notification_display_style)) {
            return false;
        }
        $url = $this->url . 'users/' . $username;
        $options = array(
            "notification_display_style" => $notification_display_style
        );
        $body = json_encode($options);
        $header = array($this->getToken());
        $data = postCurl($url, $body, $header, 'PUT');
        return resultHuanxinInfo($data);
    }

    /**
     * 设置免打扰
     * $username 用户环信ID
     * $notification_no_disturbing  是否免打扰，“0”代表免打扰关闭，“1”免打扰开启
     * $notification_no_disturbing_start 免打扰起始时间，单位是小时
     * $notification_no_disturbing_end 免打扰结束时间，单位是小时
     * return false 或者true
     */
    public function setNotDisturb($username, $notification_no_disturbing, $notification_no_disturbing_start, $notification_no_disturbing_end)
    {
        if (empty($username) || empty($notification_no_disturbing) || empty($notification_no_disturbing_start) || empty($notification_no_disturbing_end)) {
            return false;
        }
        $url = $this->url . 'users/' . $username;
        $options = array(
            "notification_no_disturbing" => $notification_no_disturbing,
            "notification_no_disturbing_start" => $notification_no_disturbing_start,
            "notification_no_disturbing_end" => $notification_no_disturbing_end,
        );
        $body = json_encode($options);
        $header = array($this->getToken());
        $data = postCurl($url, $body, $header, 'PUT');
        return resultHuanxinInfo($data);
    }

    /**
     * 添加好友
     * $username 用户环信ID
     * $friend_name  好友环信ID
     * return false 或者true
     */
    public function addFriend($username, $friend_name)
    {
        if (empty($username) || empty($friend_name)) {
            return false;
        }
        $url = $this->url . 'users/' . $username . '/contacts/users/' . $friend_name;
        $header = array($this->getToken(), 'Content-Type:application/json');
        $data = postCurl($url, '', $header, 'POST');
        return resultHuanxinInfo($data);
    }

    /**
     * 删除好友
     * $username 用户环信ID
     * $friend_name  好友环信ID
     * return false 或者true
     */
    public function deleteFriend($username, $friend_name)
    {
        if (empty($username) || empty($friend_name)) {
            return false;
        }
        $url = $this->url . 'users/' . $username . '/contacts/users/' . $friend_name;
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'DELETE');
        return resultHuanxinInfo($data);
    }

    /**
     * 查看好友列表
     * $username 用户环信ID
     * return false 或者true
     */
    public function showFriends($username)
    {
        if (empty($username)) {
            return false;
        }
        $url = $this->url . 'users/' . $username . '/contacts/users';
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'GET');
        return resultHuanxinInfo($data);
    }

    /**
     * 查看用户黑名单
     * $username 用户环信ID
     * return false 或者true
     */
    public function getBlacklist($username)
    {
        if (empty($username)) {
            return false;
        }
        $url = $this->url . 'users/' . $username . '/blocks/users';
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'GET');
        return resultHuanxinInfo($data);
    }

    /**
     * 加入黑名单
     * $username 用户环信ID
     * return false 或者true
     */
    public function addUserForBlacklist($username,$owner_username)
    {
        if (empty($username)|| empty($owner_username)) {
            return false;
        }
        $usernames['usernames'] = [
            $username
        ];
        $body = json_encode($usernames);
        $url = $this->url . 'users/' . $owner_username . '/blocks/users';
        $header = array($this->getToken());
        $data = postCurl($url, $body, $header, 'POST');
        return resultHuanxinInfo($data);
    }

    /**
     * 剔除黑名单
     * $username 用户环信ID
     * $blocked_name 黑名单环信ID
     * return false 或者true
     */
    public function deleteUserFromBlacklist($username, $blocked_name)
    {
        if (empty($username) || empty($blocked_name)) {
            return false;
        }
        $url = $this->url . 'users/' . $username . '/blocks/users/' . $blocked_name;
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'DELETE');
        return resultHuanxinInfo($data);
    }

    /**
     * 查看用户是否在线
     * $username 用户环信ID
      * return false 或者true
     */
    public function isOnline($username)
    {
        if (empty($username)) {
            return false;
        }
        $url = $this->url . 'users/' . $username . '/status';
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'GET');
        return resultHuanxinInfo($data);
    }

    /**
     * 查看用户离线消息数
     * $username 用户环信ID
     * return false 或者true
     */
    public function getOfflineMessages($username)
    {
        if (empty($username)) {
            return false;
        }
        $url = $this->url . 'users/' . $username . '/offline_msg_count';
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'GET');
        return resultHuanxinInfo($data);
    }

    /**
     * 查看用户离线消息数
     * $username 用户环信ID
     * $msg_id  消息ID
     * $type （为空就行） 是否获取环信返回的数据 （为 1 时返回成功的详细信息，为2 时返回错误的信息，为空时不返回）
     * return false 或者true
     */
    public function getOfflineMessageStatus($username, $msg_id)
    {
        if (empty($username) || empty($msg_id)) {
            return false;
        }
        $url = $this->url . 'users/' . $username . '/offline_msg_status/' . $msg_id;
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'GET');
        return resultHuanxinInfo($data);
    }

    /**
     * 禁用用户账号
     * $username 用户环信ID
     * $type （为空就行） 是否获取环信返回的数据 （为 1 时返回成功的详细信息，为2 时返回错误的信息，为空时不返回）
     * return false 或者true
     */
    public function deactiveUser($username)
    {
        if (empty($username)) {
            return false;
        }
        $url = $this->url . 'users/' . $username . '/deactivate';
        $header = array($this->getToken());
        $data = postCurl($url, '', $header);
        return resultHuanxinInfo($data);
    }

    /**
     * 解禁用户账号
     * $username 用户环信ID
     * $type （为空就行） 是否获取环信返回的数据 （为 1 时返回成功的详细信息，为2 时返回错误的信息，为空时不返回）
     * return false 或者true
     */
    public function activeUser($username)
    {
        if (empty($username)) {
            return false;
        }
        $url = $this->url . 'users/' . $username . '/activate';
        $header = array($this->getToken());
        $data = postCurl($url, '', $header);
        return resultHuanxinInfo($data);
    }

    /**
     * 强制用户下线
     * $username 用户环信ID
     * $type （为空就行） 是否获取环信返回的数据 （为 1 时返回成功的详细信息，为2 时返回错误的信息，为空时不返回）
     * return false 或者true
     */
    public function disconnectUser($username)
    {
        if (empty($username)) {
            return false;
        }
        $url = $this->url . 'users/' . $username . '/disconnect';
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'GET');
        return resultHuanxinInfo($data);
    }

    /**
     * 设置用户属性
     * $username 用户环信ID
     * $nickname 昵称
     * $avatarurl 头像
     * $phone 电话
     * $gender性别
     * $type （为空就行） 是否获取环信返回的数据 （为 1 时返回成功的详细信息，为2 时返回错误的信息，为空时不返回）
     * return false 或者true
     */
    public function setUserProperties($username, $nickname, $avatarurl, $phone, $gender)
    {
        if (empty($username) || empty($nickname) || empty($avatarurl) || empty($phone) || empty($gender)) {
            return false;
        }
        $url = $this->url . 'metadata/users/' . $username;
        $options = array(
            "nickname" => $nickname,
            "avatarurl" => $avatarurl,
            "phone" => $phone,
            "gender" => $gender,
        );
        $body = json_encode($options);
        $header = array($this->getToken());
        $data = postCurl($url, $body, $header, 'PUT');
        return resultHuanxinInfo($data);
    }

    /**
     * 获取用户属性
     * $username 用户环信ID
     * $type （为空就行） 是否获取环信返回的数据 （为 1 时返回成功的详细信息，为2 时返回错误的信息，为空时不返回）
     * return false 或者true
     */
    public function getUserProperties($username)
    {
        if (empty($username)) {
            return false;
        }
        $url = $this->url . 'metadata/users/' . $username;
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'GET');
        return resultHuanxinInfo($data);
    }

    /**
     * 删除用户属性
     * $username 用户环信ID
     * $type （为空就行） 是否获取环信返回的数据 （为 1 时返回成功的详细信息，为2 时返回错误的信息，为空时不返回）
     * return false 或者true
     */
    public function delUserProperties($username)
    {
        if (empty($username)) {
            return false;
        }
        $url = $this->url . 'metadata/users/' . $username;
        $header = array($this->getToken());
        $data = postCurl($url, '', $header, 'DELETE');
        return resultHuanxinInfo($data);
    }

}
